package cn.itcast.shiro;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyPermissionsFilter extends AuthorizationFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {

        // 获取当前的登录人
        Subject subject = this.getSubject(request, response);
        // 获取当前访问资源时，所需要权限  ["部门管理","新增部门"]
        String[] perms = (String[]) ((String[]) mappedValue);

        boolean isPermitted = false;
        if (perms != null && perms.length > 0) {
            for (String perm : perms) {
                if (subject.isPermitted(perm)) {  // 只要当前登录人，满足其中任何一个权限，放行
                    isPermitted = true;
                    break;
                }
            }
        }

        return isPermitted;
    }
}
